// pages/dj/start/strat.js
var id;
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bt:'',
    wj_info:[],
    noshare:false,
    stopdate:null,
    xzdjs:0,
    notongji:false,
  },
  userInfoHandler: function (e) {
    console.log(e);
  },
  ksdj:function(){
    wx.showLoading({
      title: '准备数据中',
    });
    var that=this;
    var stopdate = this.data.stopdate;
    console.log(stopdate);
    stopdate = new Date(stopdate.replace(/-/g, "/")).getTime();
    var nowdate = new Date().getTime();
    console.log(nowdate);
    console.log(stopdate);
    const db = wx.cloud.database()
    db.collection('user_dt').where({
      wjbh:id
    })
    .get({
      success: res => {
        {
          wx.hideLoading();
          var ydrs = res.data.length;
          if (that.data.xzdjs != 0 && ydrs >= that.data.xzdjs)
          {
            wx.showModal({
              content: '该问卷已超过最大答题人数上限',
              showCancel: false,
            });
            return false;
          }
          else
          {
            if (nowdate > stopdate) {
              wx.showModal({
                content: '该问卷已超过答题时间',
                showCancel: false,
              });
            }
            else {
              wx.navigateTo({
                url: "/pages/dj/dj/dj",
              })
            }
          }
        }
      },
      fail: err => {
        wx.hideLoading();
        wx.showModal({
          content: '处理数据异常,请重试!',
          showCancel: false,
        });
      }
      })
  },
  tjsj:function(){
    wx.navigateTo({
      url: "/pages/dj/dj_tongji/dj_tongji?id="+id,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '获取问卷数据中',
    });
    wx.hideShareMenu({});
    if (options.id != undefined) {
      id = options.id;
      app.globalData.now_wj = [];
      var that = this;
      const db = wx.cloud.database()
      db.collection('wenjuan').where({
        _id: options.id
      })
        .get({
          success: res => {
            {
              wx.hideLoading();
              if(res.data.length > 0)
              {
                console.log(res);
                var wj_info = res.data[0].wj_info;
                console.log(wj_info[0].wj_jbxx.fx);
                console.log(res.data[0].openid);
                console.log(app.globalData.openid);
                  if (res.data[0].openid != app.globalData.openid) {
                    console.log(parseInt(wj_info[0].wj_jbxx.gk));
                    if (parseInt(wj_info[0].wj_jbxx.gk) == 1) {
                      console.log("notongji");
                      that.setData({
                        notongji: true
                      })
                    }
                    if (!wj_info[0].wj_jbxx.fx) {
                      wx.hideShareMenu({});
                      console.log("noshare");
                      that.setData({
                        noshare: true
                      })
                    }
                    else{
                      wx.showShareMenu({});
                    }
                  }
                  else{
                    wx.showShareMenu({});
                  }

                console.log(wj_info);
                that.setData({
                  bt: wj_info[0].wj_jbxx.bt,
                  wj_info: wj_info,
                  stopdate: wj_info[0].wj_jbxx.startrq + " " + wj_info[0].wj_jbxx.endrq,
                  xzdjs: wj_info[0].wj_jbxx.xzdjs,
                });
                app.globalData.now_wj = wj_info;
                app.globalData.now_cfdj = wj_info[0].wj_jbxx.cfdj;
                app.globalData.now_xgdj = wj_info[0].wj_jbxx.xgdj;
                app.globalData.now_id = res.data[0]._id;
              }
              else
              {
                wx.hideLoading();
                wx.showModal({
                  content: '该问卷不存在',
                  showCancel: false,
                  success: function () {
                    wx.redirectTo({
                      url: '/pages/index/index',
                    })
                  }
                });
              }
            }
          },
          fail: err => {
            wx.hideLoading();
            console.error('[数据库] [新增记录] 失败：', err)
          }
        })
    }
    else {
      wx.showModal({
        content: '问卷参数丢失,请重试!',
        showCancel: false,
        success: function () {
          wx.redirectTo({
            url: "/pages/index/index",
          })
        }
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var that = this;
    if (!that.data.noshare) {
      console.log("尝试禁止群聊转发");
      wx.showShareMenu({
        withShareTicket: true
      })
    }
    return {
      title: '机大轻问卷-' + this.data.bt,
      path: "/pages/dj/start/strat?id=" + id, // 路径，传递参数到指定页面。
    }
  },
})